// 1. 导入
import React from 'react'
import { Card, Form, Input, Checkbox, Button, message } from 'antd'
import './index.scss'
// import logo from '@/assets/logo.png'
import logo from 'assets/logo.png'
import { login } from '@/api/user'

// 2. 创建
class Login extends React.Component {
  onFinish = async values => {
    try {
      let { mobile, code } = values
      let res = await login(mobile, code)
      console.log('结果', res.data)

      // 保存token
      localStorage.setItem('geek_token', res.data.token)

      message.success('登录成功')
      this.props.history.push('/layout')
    } catch (error) {
      message.error(error.response.data.message)
    }
  }

  render() {
    return (
      <div className="login">
        <Card className="login-container">
          <img className="login-logo" src={logo} alt="" />
          {/* 表单 */}
          <Form
            initialValues={{
              agree: true,
              mobile: '13811111111',
              code: '246810',
            }}
            onFinish={this.onFinish}
            size="large"
            validateTrigger={['onChange', 'onBlur']}
          >
            <Form.Item
              name="mobile"
              rules={[
                { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确' },
                { required: true, message: '请输入手机号' },
              ]}
            >
              <Input placeholder="请输入手机号" />
            </Form.Item>

            <Form.Item
              name="code"
              rules={[
                {
                  pattern: /^\d{6}$/,
                  message: '验证码格式不对',
                },
                { required: true, message: '请输入验证码' },
              ]}
            >
              <Input placeholder="请输入验证码" />
            </Form.Item>

            <Form.Item
              name="agree"
              valuePropName="checked"
              rules={[{ required: true, message: '请阅读协议' }]}
            >
              <Checkbox>我已阅读并同意[用户协议]和[隐私条款]</Checkbox>
            </Form.Item>

            <Form.Item>
              <Button type="primary" htmlType="submit" block>
                登录
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div>
    )
  }
}

// 3. 导出
export default Login
